﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WebApi.Services.Interface;
using WebApi.Models.Common;
using WebApi.Models.Views.Account;

namespace WebApi.Controllers
{
    /// <summary>
    /// 主控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class HomeController : BaseController
    {
        #region 构造函数

        IAccountService _accountService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="accountService"></param>
        public HomeController(IAccountService accountService)
        {
            _accountService = accountService;
        }

        #endregion

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public LoginResponse Login(LoginRequest request)
        {
            var result = _accountService.Login(request.Account, request.Password);
            return result;
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultInfoModel ResetPassword(ResetPasswordRequest request)
        {
            var result = new ResetPasswordValidator().ValidateAsync(request).Result;
            if (!result.IsValid)
            {
                return GetResult.Error(string.Join(',', result.Errors.Select(p => p.ErrorMessage)));
            }

            return _accountService.ResetPassword(request, userAccount);
        }
    }
}
